package com.xysh.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xysh.service.CipherService;
import com.xysh.service.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/utils/api/")
@Slf4j
public class UtilsControl {

    public Map<String, String> initMap() {
        Map<String, String> map = new HashMap();
        String result = FileUtil.getString("newUrl.txt");
        String[] mapStr;
        if(result.indexOf("\r\n")>-1){
            mapStr = result.split("\r\n");
        }else {
            mapStr = result.split("\n");
        }
        System.out.println("just test");
        System.out.println("just test");
        System.out.println("load url files..");
        String[] var4 = mapStr;
        int var5 = mapStr.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            String str = var4[var6];
            System.out.println(str);
            String[] split = str.split(",");
            map.put(split[0], split[1].trim().replace("\r\n", ""));
        }

        return map;
    }


    @Resource
    private CipherService cipherService;
    @PostMapping("encrypt")
    public Map<String,String> entryResult(@RequestBody JSONObject object){
        return cipherService.encrypt(object.toJSONString());
    }

    @PostMapping("decrypt")
    public String detryResult(@RequestBody JSONObject object){
        return cipherService.decrypt(object.containsKey("encryptData") ? object.getString("encryptData") : object.getString("encodeStr"), object.getString("signature"));
    }



    @PostMapping(value = "mock/{path}",consumes = MediaType.ALL_VALUE)
    public String getMockStr(@RequestBody JSONObject object,@PathVariable("path") String path){
        log.info("{}：请求参数{}",path,object.toJSONString());
        Map<String, String> map = initMap();
        String filePath = map.get("/utils/api/mock/" + path);
        String string = FileUtil.getString( filePath);
        return string.trim().replace("\r\n", "");


    }

    @GetMapping(value = "mock/{path}",consumes = {MediaType.ALL_VALUE})
    public String getMockStr(HttpServletRequest request, @PathVariable("path") String path){
        Map<String, String[]> parameterMap = request.getParameterMap();
        log.info("{}：请求参数{}",path,JSON.toJSONString(parameterMap));
        Map<String, String> map = initMap();
        String filePath = map.get("/utils/api/mock/" + path);
        String string = FileUtil.getString(filePath);
        return string.trim().replace("\r\n", "");
       //todonoting

    }
   @PostMapping(value = "mock2/{path}",consumes = {MediaType.ALL_VALUE})
    public String getMockStrFrm(@RequestParam Map<String,Object> params, @PathVariable("path") String path){
        Map<String, String> map = initMap();
        log.info("{}：请求参数{}",path,JSON.toJSONString(params));
        String filePath = map.get("/utils/api/mock/" + path);
        String string = FileUtil.getString(filePath);
        return string.trim().replace("\r\n", "");

    }



}
